package com.wm.blog_admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.dao.TSysDictTypeDAO;
import com.wm.blog_admin.query.TSysDictTypeQuery;
import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.bean.PageHelper;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TSysDictTypeDO;
import com.wm.blog_common.entity.TSysDictType;
import com.wm.blog_common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Service
public class TSysDictTypeService{
    @Autowired
    private TSysDictTypeDAO tSysDictTypeDAO;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Result<TSysDictTypeDO> selectById(Long id){
        return Result.success(BeanUtils.copyProperties(tSysDictTypeDAO.getById(id),TSysDictTypeDO.class));
    }

    /**
    * 根据ID删除
    * @param id
    * @return
    */
    public Result deleteById(Long id){
        return tSysDictTypeDAO.removeById(id)?Result.success():Result.failure();
    }

    /**
    * 添加
    * @param tSysDictType
    * @return
    */
    public Result insert(TSysDictType tSysDictType){
        return tSysDictTypeDAO.save(tSysDictType)?Result.success():Result.failure();
    }

    /**
    * 修改
    * @param tSysDictType
    * @return
    */
    public Result update(TSysDictType tSysDictType){
        return tSysDictTypeDAO.updateById(tSysDictType)?Result.success():Result.failure();
    }

    /**
    * 统计总数
    *
    * @param query
    * @return
    */
    public Result<Integer> count(TSysDictTypeQuery query) {
        return Result.success(tSysDictTypeDAO.count(this.combineQuery(query)));
    }

    /**
    * 列表查询
    * @param query
    * @return
    */
    public Result<List<TSysDictTypeDO>> list(TSysDictTypeQuery query){
        return Result.success(BeanUtils.copyListProperties(tSysDictTypeDAO.list(this.combineQuery(query)),TSysDictTypeDO.class));
    }
    /**
    * 分页查询
    * @param page
    * @return
    */
    public Result<Page<TSysDictTypeDO>> page(QueryPage page, TSysDictTypeQuery query){
        return Result.success(PageHelper.newPage(tSysDictTypeDAO.page(page,this.combineQuery(query)),TSysDictTypeDO.class));
    }

    /**
    * 组装查询条件
    *
    * @param query
    * @return
    */
    private QueryWrapper<TSysDictType> combineQuery(TSysDictTypeQuery query) {
        QueryWrapper<TSysDictType> wrapper = new QueryWrapper<>();
        //todo 查询条件 wrapper.eq("字段名","值")
        return wrapper;
    }

}







